package com.tentact.dao;

import com.tentact.vo.Employee;
import com.tentact.vo.Meeting;
import com.tentact.vo.MeetingRoom;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MeetingsDao {
    //搜索会议
    public List<Meeting> selectByroomting(Meeting meet,String meetingComm,String meetingEnd) {
        StringBuffer sql = new StringBuffer("select m.meetingid,m.MeetingName,r.RoomName,m.BeginTime,m.EndTime,m.ReservationTime,e.EmployeeName ");
        sql.append("from meeting m JOIN meetingroom r on m.RoomId = r.RoomId join employee e on m.ReservationistId = e.EmployeeId WHERE 1=1 ");
        List<Object> params =new ArrayList<Object>();
        // Meeting meeting = new Meeting(MeetingName,meetingRoom,BeginTime,EndTime,ReservationTime,employee);
        if (meet.getMeetingname() != null) {
            sql.append("and M.MeetingName like ? ");
            params.add("%"+meet.getMeetingname()+"%");
        }
        if (meet.getMeetingRoom().getRoomname() != null) {
            System.out.println("顶顶顶顶");
            sql.append("and r.Roomname like ? ");
            params.add("%"+meet.getMeetingRoom().getRoomname()+"%");
        }
        if (meet.getBegintime() != "") {
            sql.append("and m.ReservationTime >= ? ");
            params.add(meet.getBegintime());
        }
        if (meet.getEndtime() != "") {
            sql.append("and m.ReservationTime <= ? ");
            params.add(meet.getEndtime());
        }
        if (meetingComm != ""){
            sql.append("and m.BeginTime >= ? ");
            params.add(meetingComm);
        }
        if (meetingEnd != ""){
            sql.append("and m.BeginTime <= ? ");
            params.add(meetingEnd);
        }
        if (meet.getEmployee().getEmployeename() != null) {
            System.out.println("顶顶顶顶");
            System.out.println(meet.getEmployee().getEmployeename());
            sql.append("and e.EmployeeName like ? ");
            params.add("%"+meet.getEmployee().getEmployeename()+"%");
        }
        Object[] paramters =params.toArray();
        RowMapper<Meeting> rm = (rs) ->{
            Meeting meeting = new Meeting();
            try {
                meeting.setMeetingid(rs.getInt("meetingid"));
                meeting.setMeetingname(rs.getString("MeetingName"));
                MeetingRoom meetingRoom = new MeetingRoom();
                meetingRoom.setRoomname(rs.getString("RoomName"));
                meeting.setBegintime(rs.getString("BeginTime"));
                meeting.setEndtime(rs.getString("EndTime"));
                meeting.setReservationtime(rs.getString("ReservationTime"));
                Employee employee = new Employee();
                employee.setEmployeename(rs.getString("EmployeeName"));
                meeting.setMeetingRoom(meetingRoom);
                meeting.setEmployee(employee);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return meeting;
        };
        return Dbutil.executeQuery(sql.toString(),paramters,rm);
        }
}
